/**
 * 认证工具类
 * 用于管理用户token的存储、获取和删除
 */

/**
 * Token存储键名
 */
const TokenKey = 'Admin-Token';

/**
 * 获取token
 * @returns Token字符串或null
 */
export const getToken = (): string | null => {
  return localStorage.getItem(TokenKey);
};

/**
 * 设置token
 * @param access_token 访问令牌
 */
export const setToken = (access_token: string): void => {
  localStorage.setItem(TokenKey, access_token);
};

/**
 * 移除token
 */
export const removeToken = (): void => {
  localStorage.removeItem(TokenKey);
};

/**
 * 记住用户登录信息
 * @param tenantId 租户ID
 * @param username 用户名
 * @param password 密码
 * @param rememberMe 是否记住密码
 * @param clientRoleType 用户身份类型
 */
export const rememberLoginInfo = (
  tenantId: string = '000000',   // 默认租户ID为000000
  username: string,
  password: string,
  rememberMe: boolean,
  clientRoleType: string = '00000' // 默认为设计师
): void => {
  if (rememberMe) {
    localStorage.setItem('tenantId', tenantId);
    localStorage.setItem('username', username);
    localStorage.setItem('password', password);
    localStorage.setItem('clientRoleType', clientRoleType);
    localStorage.setItem('rememberMe', 'true');
  } else {
    localStorage.removeItem('tenantId');
    localStorage.removeItem('username');
    localStorage.removeItem('password');
    localStorage.removeItem('clientRoleType');
    localStorage.removeItem('rememberMe');
  }
};

/**
 * 获取记住的登录信息
 * @returns 记住的登录信息对象
 */
export const getRememberedLoginInfo = (): {
  tenantId: string;
  username: string;
  password: string;
  rememberMe: boolean;
  clientRoleType: string;
} => {
  return {
    tenantId: localStorage.getItem('tenantId') || '000000',  // 默认返回000000
    username: localStorage.getItem('username') || '',
    password: localStorage.getItem('password') || '',
    rememberMe: localStorage.getItem('rememberMe') === 'true',
    clientRoleType: localStorage.getItem('clientRoleType') || '00000' // 默认为设计师
  };
}; 